package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.dao.UserCollectionMapper;
import com.wuming.house.model.UserCollection;
import com.wuming.house.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userCollectionService")
public class UserCollectionServiceImpl extends BaseServiceImpl implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public void saveUserCollection(UserCollection collection) throws BusinessException {
        userCollectionMapper.insert(collection);
    }

    @Override
    public void removeUserCollectionById(Long id) throws BusinessException {
        userCollectionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UserCollection> getUserCollectionById(Long userId, Long houseId, Integer houseType) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        if (null != houseId) {
            params.put("houseId", houseId);
        }
        if (null != houseType) {
            params.put("houseType", houseType);
        }
        return userCollectionMapper.selectByParams(params);
    }

    @Override
    public List<UserCollection> getUserCollectionByPage(Long userid, Integer houseType, int pageIndex, int pageSize) {
        Map<String,Object> params = new HashMap<>();
        if (null!=userid){
            params.put("userid",userid);
        }
        if (null!=houseType){
            params.put("houseType",houseType);
        }
        try {
            return (List<UserCollection>) this.getPageList(UserCollectionMapper.class,"selectByParams",params,pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int getTotal(Long userid, Integer houseType) {
        Map<String,Object> params = new HashMap<>();
        if (null!=userid){
            params.put("userid",userid);
        }
        if (null!=houseType){
            params.put("houseType",houseType);
        }
        try {
            return this.getTotal(UserCollectionMapper.class,"selectTotal",params);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
